﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.Data.Entity;
using BugTracker.DataAccess.Data;

namespace BugTracker.BusinessLogic.Project.ProjectDetail
{
    public class ProjectDetailService : IProjectDetailService
    {

        private BugTrackerEntities db = null;

        public ProjectDetailService(BugTrackerEntities db)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            this.db = db;
        }

        public IList<BusinessEntities.Entities.Project.ProjectDetail> PageSort(
                                int pageIndex, int pageSize, 
                                string sortColumn, string sortDirection, 
                                out int count)
        {
            count = 0;
            count = (from models in db.Projects
                           join platforms in db.PlatformTypes on models.PlatformTypeID equals platforms.PlatformTypeID
                           join managers in db.BTUsers on models.ProjectManagerID equals managers.BTUserID
                           join versions in db.Versions on models.VersionID equals versions.VersionID
                           join builds in db.Builds on models.BuildID equals builds.BuildID
                           where models.IsDeleted == false
                           select models).Count();

            var results = (
                            from models in db.Projects
                            join platforms in db.PlatformTypes on models.PlatformTypeID equals platforms.PlatformTypeID
                            join managers in db.BTUsers on models.ProjectManagerID equals managers.BTUserID
                            join versions in db.Versions on models.VersionID equals versions.VersionID
                            join builds in db.Builds on models.BuildID equals builds.BuildID
                            where models.IsDeleted == false
                            select new BusinessEntities.Entities.Project.ProjectDetail 
                               { 
                                   ProjectID = models.ProjectID,
                                   Name = models.Name,
                                   Description = models.Description,
                                   PlatformTypeID = models.PlatformTypeID,
                                   ProjectManagerID = models.ProjectManagerID,
                                   VersionID = models.VersionID,
                                   BuildID = models.BuildID,
                                   IsActive = models.IsActive,
                                   IsDeleted = models.IsDeleted,
                                   RowGuid = models.RowGuid,
                                   CreatedOn = models.CreatedOn,
                                   ModifiedOn = models.ModifiedOn,
                                   PlatformType = platforms.Name,
                                   ProjectManager = (managers.FirstName + " " + managers.LastName),
                                   ProjectVersion = versions.Name,
                                   ProjectBuild = builds.Name
                               }
                           )
                           .OrderBy("it." + sortColumn + " " + sortDirection)
                           .Skip(pageIndex * pageSize)
                           .Take(pageSize);

            return results.ToList<BusinessEntities.Entities.Project.ProjectDetail>();
        }

        public IList<BusinessEntities.Entities.Project.ProjectDetail> PageSortWhere(
                                int pageIndex, int pageSize, 
                                string sortColumn, string sortDirection, 
                                string whereColumn, string whereClause, out int count)
        {
            throw new NotImplementedException();
        }

        public IList<BusinessEntities.Entities.Project.ProjectDetail> Report()
        {

            var results = (
                            from models in db.Projects
                            join platforms in db.PlatformTypes on models.PlatformTypeID equals platforms.PlatformTypeID
                            join managers in db.BTUsers on models.ProjectManagerID equals managers.BTUserID
                            join versions in db.Versions on models.VersionID equals versions.VersionID
                            join builds in db.Builds on models.BuildID equals builds.BuildID
                            where models.IsDeleted == false
                            select new BusinessEntities.Entities.Project.ProjectDetail
                            {
                                ProjectID = models.ProjectID,
                                Name = models.Name,
                                Description = models.Description,
                                IsActive = models.IsActive,
                                IsDeleted = models.IsDeleted,
                                CreatedOn = models.CreatedOn,
                                ModifiedOn = models.ModifiedOn,
                                PlatformType = platforms.Name,
                                ProjectManager = (managers.FirstName + " " + managers.LastName),
                                ProjectVersion = versions.Name,
                                ProjectBuild = builds.Name
                            }
                           );
            return results.ToList<BusinessEntities.Entities.Project.ProjectDetail>();
        }
    }
}
